package com.zhonghan.recharge.task;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.zhonghan.common.bean.Result;
import com.zhonghan.common.utils.JiSuOrder;
import com.zhonghan.recharge.dao.ZhCardReceiveDao;
import com.zhonghan.recharge.dao.ZhFillCardDao;
import com.zhonghan.recharge.dao.ZhGascardOrderDao;
import com.zhonghan.recharge.pojo.ZhCardReceive;
import com.zhonghan.recharge.service.impl.jisu.JiSuRecharge;

public class JiSuRechargeTask {
	private static final Logger logger = LogManager.getLogger(JiSuRechargeTask.class);
	
	@Autowired
    private ZhFillCardDao zhFillCardDao;
	@Autowired
    private ZhGascardOrderDao zhGascardOrderDao;
	
	@Autowired
	private ZhCardReceiveDao zhCardReceiveDao;
	
	
	public void toJiSuRecharge(String str) {
		
		Result result = new Result();
		
		String strs[] = str.split(";");
		String orderId = strs[0];
		String gasnumber = strs[1];
		String gasname = strs[2];
		String gasphone = strs[3];
		Integer facePrice = Integer.parseInt(strs[4]);
		String recardno = strs[5];
		
		ZhCardReceive zhCardReceive = new ZhCardReceive();//zhCardReceiveDao.selectZhCardReceiveBySequenceII(recardno);
		zhCardReceive.setPortAppkey("2980c26878d61dc2");
		zhCardReceive.setPortAppsecret("5KW9IGrEak1QsP34q2tfGDXrWK9FpsQx");
		zhCardReceive.setPortUrl("http://api.jisuapi.com/fuelcardrecharge/recharge");
		
		Result oil_result = null;
		if(zhCardReceive != null) {
			oil_result = JiSuRecharge.toRechargeII(orderId, gasnumber, gasname, gasphone, facePrice,zhCardReceive.getPortAppkey(),zhCardReceive.getPortAppsecret());
		}
		if(oil_result != null && "1".equals(oil_result.getCode())) {
			
			result.setCode("1");
			result.setMsg("油卡充值成功!");
			/**
			 * 更改订单状态为充值中， 获取 极速订单号
			 */
			JiSuOrder data = (JiSuOrder)oil_result.getData();
			zhGascardOrderDao.updateRechargeStateByOrderNo(orderId, "0",data.getOrderno());
			
			logger.info("订单加入定时器。。。。。");
			TaskList.JISUTASK.put(data.getOrderno(), orderId+";"+zhCardReceive.getPortAppkey());
			
		}else {
			logger.info("获取极速订单失败..................");
			logger.info("油卡充值失败..............");
			logger.info("orderId"+orderId+",");
			/**
			 * 更改卡片状态
			 */
			int num = zhGascardOrderDao.updateRechargeStateByOrderNo(orderId, "9","");
			if(num>=1) {
				zhFillCardDao.updateStatusBySequence("1", recardno);
			}
			result.setCode("0");
			result.setMsg("油卡充值失败!");
			//throw new RuntimeException(JSON.toJSONString(oil_result));
		}
	}
}
